package vn.student.controller;

import java.util.ArrayList;
import vn.student.AStudentDB;
import vn.student.R;
import vn.student.ViewClassDetail;
import vn.student.mylib.Class;
import vn.student.mylib.Constants;
import vn.student.mylib.Course;
import vn.student.mylib.ListClassAdapter;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.ContextMenu;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.View;
import android.view.View.OnCreateContextMenuListener;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;

public class ViewCourseDetailController {
	Activity act = null;
	public String id;
	AStudentDB myDB = null;
	public Course course = null;
	public ViewCourseDetailController(Activity act,String id){
		this.act = act;
		this.id = id;
		myDB = new AStudentDB(act); 
	}
	public void showCourseDetail(String id) {
		course = myDB.getCourseDetail(id);
		if(course==null)
			return;
		TextView tmpLabel = (TextView) act.findViewById(R.id.courseNameLabel);
		tmpLabel.setText(course.courseName);
		tmpLabel = (TextView) act.findViewById(R.id.dateStart);
		tmpLabel.setText("From " + course.dateStart +" to " + course.dateEnd);

		
		this.showClassList(); 
	}
	public void showClassList(){
		ArrayList<Class> listClass = myDB.getCourseClassList(id);
		if(listClass==null||listClass.size()==0)
			return;
		ListView listClassView = (ListView)act.findViewById(R.id.listClassView);
		listClassView.getLayoutParams().height=107*listClass.size()+10;
		RelativeLayout tmp = (RelativeLayout) act.findViewById(R.id.addNewClassBtn);
		tmp.setBackgroundResource(R.drawable.class_row_top);
		listClassView.setAdapter(new ListClassAdapter(act,listClass,true));
		listClassView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
			
			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
					long arg3) {
				Intent intent = new Intent(act,ViewClassDetail.class);
				Bundle bundle = new Bundle();
				ListClassAdapter adapter = (ListClassAdapter) arg0.getAdapter();
				ArrayList<Class> list = adapter.list;
				bundle.putString("id", list.get(arg2).id);
				bundle.putBoolean("isViewStudentAss", true);
				intent.putExtras(bundle);
				act.startActivityForResult(intent, Constants.ViewClassDetailRequestCode);
			}
		});
		listClassView.setOnCreateContextMenuListener(new OnCreateContextMenuListener()  {
			@Override
			public void onCreateContextMenu(ContextMenu menu, View v,
					ContextMenuInfo menuInfo) {
				menu.add(0,Constants.COURSE_DETAIL_CONTEXTMENU_ADD_CLASS,0, "Add new");
				menu.add(0,Constants.COURSE_DETAIL_CONTEXTMENU_EDIT_CLASS,1, "Edit");
				menu.add(0,Constants.COURSE_DETAIL_CONTEXTMENU_DELETE_CLASS,2, "Delete");
			}
       });
	}
	public boolean removeClass(String id) {
		return myDB.removeClass(id);
	}
}
